package i2.application.checklist.selenium.pages.administration.applications;

import i2.application.checklist.entities.Application;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ListLivraisonsPage {

	private static final Logger logger = LoggerFactory.getLogger(ListLivraisonsPage.class);
	
	protected WebDriver driver;
	
	@FindBy(name="application.nom")
	private WebElement creerLivraisonLink;	

	@FindBy(name="application.description")
	private WebElement description;	

	@FindBy(id="Valider")
	private WebElement valider;	

	public ListLivraisonsPage(WebDriver driver) {
		this.driver = driver;
	}
	
	public void open(String url) {
		driver.get(url);
	}

	public void close() {
		driver.quit();
	}
	
	public String getTitle() {
		return driver.getTitle();
	}

	public void create(Application application) {
		logger.info("create {}",application);
		description.sendKeys(application.getDescription());
		valider.click();
	}

	public void clickOnValider() {
		valider.click();
	}

	public List<String> getErrors() {
		WebElement errorsList = driver.findElement(By.className("errorMessage"));
		List<WebElement> errors = errorsList.findElements(By.tagName("li"));
		
		List<String> errorsStringList = new ArrayList<String>();
		for (WebElement error : errors) {
			logger.info("erreur de validation : {}",error.getText());
			errorsStringList.add(error.getText());
		}
		
		return errorsStringList;
	}
}